package com.xh3140.xwidget.app.dialog;

import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.xh3140.xwidget.app.R;
import com.xh3140.xwidget.app.databinding.ActivityDialogBinding;
import com.xh3140.xwidget.dialog.ContentDialog;
import com.xh3140.xwidget.dialog.InputDialog;
import com.xh3140.xwidget.dialog.ListDialog;
import com.xh3140.xwidget.dialog.LoadingDialog;
import com.xh3140.xwidget.toast.Toast;

import java.util.ArrayList;
import java.util.List;

public class DialogActivity extends AppCompatActivity {
  private ActivityDialogBinding binding;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_dialog);
    initListener();
  }

  private void initListener() {
    binding.buttonContentDialog.setOnClickListener(v ->
            new ContentDialog.Builder(this)
                    .setTitleText("长恨歌")
                    .setSubtitleText("白居易")
                    .setContentText(getString(R.string.chang_hen_ge))
                    .setPositiveButtonText("朕已阅")
                    .build()
                    .show());

    binding.buttonInputDialog.setOnClickListener(v ->
            new InputDialog.Builder(this)
                    .setTitleText("你的名字")
                    .setInputText("张三")
                    .setInputHint("爪巴")
                    .setNegativeButtonText("取消")
                    .setPositiveButtonText("确定")
                    .setButtonOnClickListener((dialog, button) -> {
                      if (button.isPositive()) {
                        String name = String.valueOf(dialog.getInputText());
                        if (name.isEmpty()) {
                          Toast.normal(this, "没有输入").show();
                        } else {
                          Toast.normal(this, "你的名字是:" + name).show();
                        }
                      }
                      dialog.dismiss();
                    })
                    .build()
                    .show());

    String[] provinces = {
            "北京市", "重庆市", "天津市", "上海市",
            "广东省", "山东省", "江苏省", "河南省", "河北省", "浙江省", "陕西省", "湖南省", "福建省",
            "云南省", "四川省", "安徽省", "海南省", "江西省", "湖北省", "山西省", "辽宁省", "台湾省", "贵州省",
            "甘肃省", "青海省", "黑龙江省", "吉林省",
            "香港特别行政区", "澳门特别行政区",
            "内蒙古自治区", "新疆维吾尔自治区", "西藏自治区", "广西壮族自治区", "宁夏回族自治区"};

    binding.buttonListSingleDialog.setOnClickListener(v1 ->
            new ListDialog.Builder(this, provinces.length)
                    .setTitleText("你的居住地")
                    .setSubtitleText("中华人民共和国行政区划")
                    .setListItemsText(0, provinces)
                    .setNegativeButtonText("取消")
                    .setPositiveButtonText("确定")
                    .setButtonOnClickListener((dialog, button) -> {
                      if (button.isPositive()) {
                        int index = dialog.getCheckedIndex();
                        if (index >= 0) {
                          Toast.normal(this, "你居住在:" + provinces[index]).show();
                        } else {
                          Toast.normal(this, "没有选中任何项目").show();
                        }
                      }
                      dialog.dismiss();
                    })
                    .build()
                    .show());


    String[] fruits = {"苹果", "香蕉", "橘子", "葡萄", "菠萝", "桃子", "榴莲"};
    List<Integer> defSelect = new ArrayList<>();
    defSelect.add(0);
    defSelect.add(1);
    defSelect.add(2);

    binding.buttonListMultipleDialog.setOnClickListener(v12 ->
            new ListDialog.Builder(this, fruits.length)
                    .setTitleText("你喜欢的水果有？")
                    .setListItemsText(defSelect, fruits)
                    .setNegativeButtonText("取消")
                    .setPositiveButtonText("确定")
                    .setButtonOnClickListener((dialog, button) -> {
                      if (button.isPositive()) {
                        List<Integer> indexList = dialog.getCheckedIndexList();
                        ArrayList<String> list = new ArrayList<>();
                        for (int index : indexList) {
                          list.add(fruits[index]);
                        }
                        if (list.isEmpty()) {
                          Toast.normal(this, "没有选中任何项目").show();
                        } else {
                          Toast.normal(this, "你喜欢的水果有:" + list).show();
                        }
                      }
                      dialog.dismiss();
                    })
                    .build()
                    .show());

    binding.buttonLoadingSpinnerDialog.setOnClickListener(v13 ->
            new LoadingDialog.Builder(this)
                    .setLoadingSpinner(true)
                    .setLoadingSubtitleText("加载中...")
                    .build()
                    .show());

    binding.buttonLoadingHorizontalDialog.setOnClickListener(v13 ->
            new LoadingDialog.Builder(this)
                    .setTitleText("等待对话框")
                    .setLoadingSpinner(false)
                    .setLoadingSubtitleText("加载中...")
                    .setLoadingTintColor(Color.RED)
                    .build()
                    .show());
  }
}